﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.Generic;



using System.Xml.Linq;

using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;


using System.Device.Location;
using System.Runtime.Serialization;
using first_learn.GeocodeService;
using System.Text.RegularExpressions;

using System.IO;
using System.Text;
using System.Windows;
using SocketEx;
using System.Windows.Media.Imaging;

namespace first_learn
{
   
    public partial class ViewPage : PhoneApplicationPage
    {
        public string id;
        public int cmt_num = 0;
        List<TextBlock>Text_List;
       int number = 0;
       String serverAddress = "sslab13.cs.purdue.edu";
       int port = 4546;
      //String name_text_Block = "Tblock_";
        public ViewPage()
        {

            InitializeComponent();

            
            
          
           //System.Windows.Media.Imaging.BitmapImage myImage = new BitmapImage();
           //myImage.UriSource = new Uri("/first-learn;component/images/share.jpg", UriKind.Relative);
           //View_Image.Source = new BitmapImage(new Uri("/first-learn;component/images/share.jpg", UriKind.Relative));
        }

        public string[] interpret_getcomment(string input)
        {


            string[] lines = Regex.Split(input, "#");
            int len_output = lines.Length / 2;
            string[] output = new string[len_output];
            for (int i = 1, j = 0; i < lines.Length; i += 2, j++)
            {
                output[j] = lines[i];
            }
            return output;
        }
        public string[] interpret_Get_time(string input)
        {

            string[] line_whole = Regex.Split(input, " ");
            int num = Convert.ToInt32(line_whole[1]);

            string[] output = new string[num];
            if (num != 0)
            {

                //   string  t_head=input.Substring(0, 13);
                string t_tail = input.Remove(0, 13);

                string[] lines = Regex.Split(t_tail, "&&");


                for (int i = 0; i < lines.Length - 1; i++)
                {
                    System.Console.WriteLine(lines[i]);

                    string[] s_lines = Regex.Split(lines[i], "#");
                    output[i] = s_lines[2];
                }
            }
            return output;
        }

        public void set_Comment(string []cmt, string []cmt_time) {
            
            System.Console.WriteLine(cmt.Length);
            //Comment_Edit.Text =System.Convert.ToString( cmt_time.Length);
            for (int i = cmt_num; i < cmt.Length; i++ )
            {

                TextBlock t_cmt = new TextBlock();
                TextBlock t_cmt_time = new TextBlock();

                t_cmt.FontSize = 36;
                t_cmt.Text = cmt[i];
                t_cmt_time.FontSize = 18;
                t_cmt_time.Text = cmt_time[i];
                // t_cmt.Width = 240;
                t_cmt.TextWrapping = TextWrapping.Wrap;
                //   t_cmt.Height =Auto;
                Comment_Area.Children.Add(t_cmt);
                Comment_Area.Children.Add(t_cmt_time);

                
            }
            cmt_num = cmt.Length;

         //   Text_List.Add(a);

         
        }

        private void Click_star(object sender, RoutedEventArgs e)
        {
           
       //   Image_Like.ImageSource  = new BitmapImage(new Uri("/first-learn;component/images/star_changed.png", UriKind.Relative));
      
            int count_before = Convert.ToInt32(Like_Count.Text);
            count_before++;
            String count_After = count_before.ToString();
            Like_Count.Text=count_After;
           Button_Like.IsEnabled = false;
         
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void Send_Comment(object sender, RoutedEventArgs e)
        {
            var connection = CreateConnection();
            var stream = connection.GetStream();

            var reader = new StreamReader(stream);
            var writer = new StreamWriter(stream);
            var request = "SEND COMMENTINFO " + id + " " + Comment_Edit.Text;

            writer.WriteLine(request);
            writer.Flush();

            refreshComments();

        }


        private TcpClient CreateConnection()
        {
            var connection = new TcpClient(serverAddress, port);
            return connection;
        }



        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            id = "";
            NavigationContext.QueryString.TryGetValue("value", out id);
            View_Image.Source = new BitmapImage(new Uri("http://sslab13.cs.purdue.edu:4546/"+id));

            refreshComments();
        }

        void refreshComments(){
            var connection = CreateConnection();
            var stream = connection.GetStream();

            var reader = new StreamReader(stream);
            var writer = new StreamWriter(stream);
            var request = "INFO COMMENT "+id;

            writer.WriteLine(request);
            writer.Flush();

            var fullMessage = new StringBuilder();

            string message;
            while ((message = reader.ReadLine()) != null)
            {
                if (string.IsNullOrWhiteSpace(message))
                    break;

                fullMessage.AppendLine(message);
            }

            string test = fullMessage.ToString();
            test = test.Substring(0, test.Length - 2);

            Like_Count.Text = "6";
            //string test = "COMMENTINFO 5 1#qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq#20010201&&2#aaaaaa#2003.223.32&&3#ssssss#2009.11.2.3.0&&4#ddddd#2008.1.24.2&&5#eeeee#2012.112.132&&";
            
            
            string[] cmt = interpret_getcomment(test);
            string[] cmt_time = interpret_Get_time(test);
            removeComments();
            set_Comment(cmt, cmt_time);

        }

        void removeComments()
        {
            //for (int i = 0; i < cmt_num; i++)
                



        }
    }
}